<template>
  <div>
    <div class="header">
      <van-icon name="arrow-left" class="searchicon" @click="onClickLeft" />
      <van-search
        v-model="searchkey"
        shape="round"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
      >
        <template #action>
          <div @click="onSearch">搜索</div>
        </template>
      </van-search>
    </div>
    <div class="defaultshow" v-if="results.length==0">
      <div class="history">
        <h5>历史记录</h5>
        <span
          @click="sendSearch('history',index)"
          v-for="(item,index) in history"
          :key="index"
        >{{item}}</span>
      </div>
      <div class="hot">
        <h5>热门搜索</h5>
        <ul>
          <li @click="sendSearch('hot',index)" v-for="(item,index) in hot" :key="index">
            <p>{{item}}</p>
          </li>
        </ul>
      </div>
    </div>
    <div class="key">
      <van-cell title="单元格" is-link />
      <van-cell title="单元格" is-link />
      <van-cell title="单元格" is-link />
      <van-cell title="单元格" is-link />
      <van-cell title="单元格" is-link />
      <van-cell title="单元格" is-link />
      <van-cell />
    </div>

    <Article :postData="post" v-for="post in results" :key="post.id" />
  </div>
</template>
<script>
import Article from '../components/Article'
export default {
  components: {
    Article
  },
  data() {
    return {
      searchkey: '',
      results:[],
      history:[],
      hot:['关晓彤','美女','阿信']
    }
  },
  mounted() {
    const history = JSON.parse(localStorage.getItem('history'))
    if(history){
 this.history= history
    }
   
  },
  methods: {
    // 搜索
    async onSearch() {
      if(this.searchkey.trim().length===0){
        return;
      }
      const {data:res} = await this.$http.get('/post_search',{
        params:{
          keyword:this.searchkey
        }
      })
      this.results = res.data
      console.log(123)
      const index = this.history.indexOf(this.searchkey)
      if(index===-1){
        this.history.push(this.searchkey)
      }
      
    },
    onClickLeft() {
      if(this.searchkey.trim().length!==0){
        this.searchkey =''
      }else{
        this.$router.back()
      }
    },
    sendSearch(text,i){
      if(text ==='history'){
        this.searchkey = this.history[i]
      }else if(text ==='hot'){
        this.searchkey = this.hot[i]
      }
      this.onSearch()
    }
  },
  watch: {
    searchkey(){
      if(this.searchkey.trim().length===0){
        this.results=[]
      }
    },
    history(){
      localStorage.setItem('history',JSON.stringify(this.history))
    }
  },
}
</script>
<style lang="less" scoped>
.defaultshow {
  // display: none;
}
.header {
  padding: 0 2.778vw;
  display: flex;
  align-items: center;
  .searchicon {
    font-size: 4.444vw;
    color: #9090fa;
  }
  .van-search {
    width: 100%;
    background: transparent;
  }
}
.history {
  margin: 1.389vw 5.556vw;
  line-height: 9.722vw;
  border-bottom: 0.278vw solid #eee;
  overflow: hidden;
  span {
    font-size: 3.889vw;
    margin: 1.389vw 4.167vw 1.389vw 0;
    background: #f7f8fa;
    float: left;
    line-height: 6.944vw;
    height: 6.944vw;
    padding: 0 2.778vw;
    border-radius: 2.778vw;
  }
}
.hot {
  line-height: 9.722vw;
  margin-top: 2.778vw;
  padding: 0 5.556vw;
  overflow: hidden;
  li {
    float: left;
    width: 50%;
    p {
      font-size: 3.889vw;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
.key {
  display: none;
  .van-cell {
    padding-right: 0;
  }
  padding-right: 4.167vw;
}
</style>